import React, { useEffect, useState } from 'react'
import { fillZero, week } from '@/components/GetTime/getTime'

export default function Index (props) {

  const getTime = () => {
    const myDate = new Date()
    const myYear = myDate.getFullYear() //获取完整的年份(4位,1970-????)
    const myMonth = myDate.getMonth() + 1 //获取当前月份(0-11,0代表1月)
    const myToday = myDate.getDate() //获取当前日(1-31)
    const myDay = myDate.getDay() //获取当前星期X(0-6,0代表星期天)
    const myHour = myDate.getHours() //获取当前小时数(0-23)
    const myMinute = myDate.getMinutes() //获取当前分钟数(0-59)
    const mySecond = myDate.getSeconds() //获取当前秒数(0-59)

    return myYear + '年' + fillZero(myMonth) + '月' + fillZero(myToday) + '日 ' + ' ' + fillZero(myHour) + ':' +
      fillZero(myMinute) + ':' + fillZero(mySecond) + ' ' + week[myDay]
  }

  const [data, setData] = useState(getTime())

  useEffect( () => {
    let timer = setInterval(() => {
      setData(getTime())
    }, 1000)

    return () => {
      clearInterval(timer)
    }
  }, [])

  return (
    <>{data}</>
  )
}
